package com.competitions.videoedithelper.controller;

import com.alibaba.fastjson.JSONObject;
import com.competitions.videoedithelper.entity.ResponseModel;
import com.competitions.videoedithelper.service.impl.VideoProcessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/***
 * @author 盛旭雯
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/process")
public class VideoProcessController {
    private VideoProcessServiceImpl videoProcessServiceImpl;

    @Autowired
    public VideoProcessController(VideoProcessServiceImpl videoProcessServiceImpl) {
        this.videoProcessServiceImpl = videoProcessServiceImpl;
    }

    @RequestMapping(value = "/extract",method = RequestMethod.GET)
    public ResponseModel extractAudio(@RequestParam("videoId")int videoId) {
        return videoProcessServiceImpl.extractAudio(videoId);
    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel modifyAudio(int audioId, MultipartFile part, String begin, String end) {
        return videoProcessServiceImpl.modifyAudio(audioId, part, begin, end);
    }

    @RequestMapping(value = "/modifySeg",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel modifyAudioSeg(int sourceId, String begin1, String end1,MultipartFile part, String begin2, String end2) {
        if(("".equals(begin1)||begin1==null)&&("".equals(end1)||end1==null)){
            return videoProcessServiceImpl.modifyAudio(sourceId, part, begin2, end2);
        }else {
            return videoProcessServiceImpl.modifyAudioSeg(sourceId, begin1, end1, part, begin2, end2);
        }
    }

    @RequestMapping(value = "/cut",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel cutAudio(int sourceId, String filePath, String begin, String end) {
        return videoProcessServiceImpl.cutAudio(sourceId, filePath, begin, end);
    }

    @RequestMapping(value = "/batch",method = RequestMethod.POST)
    public ResponseModel batchModification(@RequestBody JSONObject data) {
        return videoProcessServiceImpl.batchModification(data);
    }

    @RequestMapping(value = "/selectEditedAudios",method = RequestMethod.GET)
    public ResponseModel selectEditedAudios(@RequestParam("videoId") int videoId) {
        return videoProcessServiceImpl.selectEditedAudios(videoId);
    }

    @RequestMapping(value = "/deleteEditedAudio",method = RequestMethod.GET)
    public ResponseModel deleteEditedAudio(@RequestParam("audioId") int audioId) {
        return videoProcessServiceImpl.deleteEditedAudio(audioId);
    }

    @RequestMapping(value = "/combine",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel combineVideo(int videoId, String audioPath) {
        return videoProcessServiceImpl.combineVideo(videoId, audioPath);
    }
}
